/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GUIRemoveFuncionario.java
 *
 * Created on 27/11/2011, 15:33:43
 */

package GUI;

/**
 *
 * @author fernando
 */

import javax.swing.table.DefaultTableModel;

import tp1POO.Verifica;
import tp1POO.Persistencia.GerenciadorObjetos;
import tp1POO.Persistencia.Pesquisa;
import tp1POO.Modelo.*;
import java.util.Vector;
import javax.swing.JOptionPane;
import tp1POO.GestorLog;

public class GUIRemoveFuncionario extends javax.swing.JFrame {

	/** Creates new form GUIRemoveFuncionario */
	public GUIRemoveFuncionario() {
		initComponents();
		preencherJTable();
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jCancel = new javax.swing.JButton();
        jRemove = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTableRemove = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Remover Funcionário");

        jCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/icoErro.gif"))); // NOI18N
        jCancel.setText("Fechar");
        jCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCancelActionPerformed(evt);
            }
        });

        jRemove.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/deletar.gif"))); // NOI18N
        jRemove.setText("Remover");
        jRemove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRemoveActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Selecione o funcionario a ser removido", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Dialog", 1, 14))); // NOI18N

        jTableRemove.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Nome", "CPF", "Cargo "
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTableRemove);
        jTableRemove.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(21, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(51, 51, 51))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jRemove)
                    .addComponent(jCancel, javax.swing.GroupLayout.DEFAULT_SIZE, 111, Short.MAX_VALUE))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jCancel, jRemove});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, 0, 163, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(45, 45, 45)
                        .addComponent(jRemove)
                        .addGap(18, 18, 18)
                        .addComponent(jCancel)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jCancel, jRemove});

        pack();
    }// </editor-fold>//GEN-END:initComponents

	private void erroRemoveFuncionario(String msg){

		JOptionPane.showMessageDialog(jTableRemove,msg, "Error", JOptionPane.ERROR_MESSAGE);

	}

	private void jCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCancelActionPerformed
		GestorLog.obterInstancia().inserirMsgLog("Funcionalidade de Remover funcionário foi fechada("+GuiCreator.obterInstancia().getGuiFuncionalidades().getFuncionario().getCpf()+")");
		GuiCreator.obterInstancia().removerGuiRemFuncionario();

	}//GEN-LAST:event_jCancelActionPerformed

	private void jRemoveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRemoveActionPerformed
		DefaultTableModel modelo = (DefaultTableModel)jTableRemove.getModel();
		int linha = jTableRemove.getSelectedRow();

		if (jTableRemove.getSelectedRow()>=0){

			String cpf = (String) jTableRemove.getValueAt(linha, 1);

			Funcionario funcionario = Pesquisa.obterFuncionario(cpf);

			if (funcionario != null) {
				Pesquisa.removeFuncionario(cpf);
				modelo.removeRow(linha);
				GestorLog.obterInstancia().inserirMsgLog(GuiCreator.obterInstancia().getGuiFuncionalidades().getFuncionario().getCpf()+" - Funcionário removido com sucesso!\n\tNome : "+
						funcionario.getNome()+"\n\tCargo : "+funcionario.getCargo()+"\n\tCPF : "+
						funcionario.getCpf());
				JOptionPane.showMessageDialog(jPanel1,"Funcionário removido com Sucesso!", "Sucesso", JOptionPane.INFORMATION_MESSAGE);
			}
		} else{
			erroRemoveFuncionario("ERRO : Nenhum funcionário foi selecionado!");
		}
	}//GEN-LAST:event_jRemoveActionPerformed

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new GUIRemoveFuncionario().setVisible(true);
			}
		});
	}

	private void preencherJTable(){
		DefaultTableModel modelo = (DefaultTableModel)jTableRemove.getModel();
		modelo.setNumRows(0);
		Funcionario func = new Funcionario(Funcionario.Cargo.Auxiliar);

		Vector<ObjetoPOO> funcionario = GerenciadorObjetos.obterInstancia().obterObjs(func);

		for (int i=0;i<funcionario.size();i++){
			func = (Funcionario) funcionario.get(i);
			modelo.addRow(new Object[] {func.getNome(),func.getCpf(),func.getCargo()});
		}
	}

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jCancel;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton jRemove;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTableRemove;
    // End of variables declaration//GEN-END:variables

}
